package com.gopay.bis.web.controller.ebank;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.CiticConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.citic.ebank.verify.CiticB2bStreamVerify;
import com.gopay.common.domain.bank.citic.ebank.verify.RowVerify;

/**
 * @CiticB2bFrontRes.java
 * @author wanggang
 * @2013-12-10 下午5:06:49 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class CiticB2bFrontRes extends BankResBase {

	private static Log logger = LogFactory.getLog(CiticB2bFrontRes.class);

	@RequestMapping(value = "/citicB2bFrontRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- CiticB2bFrontRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		BaseMessage base = new BaseMessage();
		// 银行码赋值
		base.setBank(BankConstants.BANK_LOGO_CITIC);
		base.setSignMessage(request.getParameter("SIGNRESMSG"));
		//银行业务产品01企业网银支付
		base.setBankBusProd(BisBankBusinessProd._01);
		// 银行接口通道信息 00: 直连
		base.setBankIntrChann(BisBankInterfaceChannel._00);
		base.setFront(true);
		return base;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		CiticB2bStreamVerify stream = (CiticB2bStreamVerify) result.getDefaultModel();
		RowVerify row = stream.getTran().getRows().get(0);
		if (CiticConstants.ORDER_STATUS_SUCCESS.equals(row.getStt())) {
			orderInfo.setOrderId(row.getOrderNo());
			orderInfo.setAmt(row.getTranAmt());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(row.getOrderNo());
			orderInfo.setAmt(row.getTranAmt());
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg(request.getParameter("resMsg"));
		}
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if(StringUtils.isEmpty(request.getParameter("SIGNRESMSG"))){
			return false;
		}
		return true;
	}
}
